﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using System.Collections.Generic;
using Cosmetic.DataEntity;

public partial class Controls_ProductCategoryByProducts : System.Web.UI.UserControl
{
	private CategoryManager m_CategoryManager	= new CategoryManager();
	private ProductManager m_ProductManager		= new ProductManager();

	public int ShowCount { get; set; }

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			this.BindCategory();
		}
	}

	private void BindCategory()
	{
		Dictionary<Category, int> theCategoryDict	= new Dictionary<Category,int>();
		
		foreach (Category theCategory in this.m_CategoryManager.GetSubCateogoriesBySupCategory(this.m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.Category_L)))
		{
			int theValue	= 0;
			foreach (Category theSubCategory in theCategory.SubCategories)
			{
				theValue	+= this.m_ProductManager.GetProductByCategory(theSubCategory).Count;
			}
			theCategoryDict.Add(theCategory, theValue);
		}
		
		this.rptCategory.DataSource		= theCategoryDict.OrderByDescending(theCategory => theCategory.Value).Take(this.ShowCount);
		this.rptCategory.DataBind();
	}
}
